<template>
  <div
    class="ssologin"
    v-loading="loading"
    element-loading-text="登录中请稍等..."
  ></div>
</template>

<script lang="ts" setup>
import { getUserInfo, ssoLogin, getTenant } from '@/services/login'
import { onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '@/stores/modules/user'
const loading = ref(false)
const store = useUserStore()
const router = useRouter()
const route = useRoute()

const getssoloing = async () => {
  console.log('路由来拿', route.query)
  loading.value = true
  const ssoobj = JSON.parse(JSON.stringify(route.query))
  delete ssoobj.url
  localStorage.setItem('ssoobj', JSON.stringify(ssoobj))
  const res = await ssoLogin(ssoobj)
  if (res.code == 200) {
    store.setToken(res.data.access_token)
    const userRes: any = await getUserInfo()
    if (userRes.code == 200) {
      const tenantRes = await getTenant(userRes.data.user.tenantId)
      // 存租户信息
      store.setTenant(tenantRes.data)
      store.setUserInfo(userRes.data.user)
      store.setPermissions(userRes.data.permissions)
      store.setRoles(userRes.data.roles)
      loading.value = false
      if (route.query.url) {
        router.push(`/${route.query.url}`)
      } else {
        router.push('/')
      }
    }
  }
}
onMounted(() => {
  getssoloing()
})
</script>

<style lang="scss" scoped>
.ssologin {
  width: 100%;
  height: 100vh;
}
</style>
